/*
 * Created by SharpDevelop.
 * User: nebulom
 * Date: 9/3/2009
 * Time: 9:45 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using PilotFish.Model;

namespace PilotFish.Gui
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		static MainForm instance = new MainForm();
		
		public static MainForm Instance {
			get { return instance; }
			set { instance = value; }
		}
		
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			AttachCommands();
		}
		
		[System.ComponentModel.EditorBrowsableAttribute()]
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			this.Visible = true;
			Login();
		}
		
		void Login()
		{
			menuStrip1.Enabled = toolStrip1.Visible = statusStrip1.Visible = openWindowListPane1.Visible = false;
			if (AddDialog(new LoginForm()) == DialogResult.Cancel) {
				Close();
			} else {
				menuStrip1.Enabled = toolStrip1.Visible = statusStrip1.Visible = openWindowListPane1.Visible = true;
				this.IsMdiContainer = true;
				homePageToolStripMenuItem.PerformClick();
			}
		}
		
		void AttachCommands()
		{
			clinicInformationToolStripMenuItem.Click += delegate { AddDialog(new ClinicForm(ClinicSingleton.Instance)); };
			setupUsersToolStripMenuItem.Click += delegate { AddDialog(new UserListForm()); };
			changePasswordToolStripMenuItem.Click += delegate { AddDialog(new ChangePasswordForm()); };
			toDoListToolStripMenuItem.Click += delegate { AddChild(new ToDoListForm()); };
			
			patientListToolStripMenuItem.Click += delegate { AddChild(new PatientListForm()); };
			doctorListToolStripMenuItem.Click += delegate { AddChild(new DoctorListForm()); };
		}
		
		void LogOff()
		{
			CloseAllChildren(); Login();
		}
		
		void CloseAllChildren()
		{
			foreach (Form f in this.MdiChildren) {
				f.Close();
			}
		}
		
		public void AddChild(Form form, string text)
		{
			form.Text = text;
			AddChild(form);
		}
		
		public void AddChild(Form form)
		{
			form.MdiParent = this;
			form.WindowState = FormWindowState.Maximized;
			openWindowListPane1.AddForm(form);
			form.Show();
		}
		
		public DialogResult AddDialog(Form form)
		{
			form.StartPosition = FormStartPosition.CenterParent;
			form.FormBorderStyle = FormBorderStyle.FixedDialog;
			form.KeyPreview = true;
			form.MinimizeBox = form.MaximizeBox = form.ShowInTaskbar = false;
			form.KeyDown += delegate(object sender, KeyEventArgs e) { 
				if (e.KeyCode == Keys.Escape) {
					form.Close();
				}
			};
			return form.ShowDialog(this);
		}
	}
}
